<?php 
require_once 'common/common.inc.php';
require_once 'common/mail.inc.php';

$date_str = isset($_REQUEST["date_str"]) ? $_REQUEST["date_str"] : "";
$reservation_id = isset($_REQUEST["reservation_id"]) ? $_REQUEST["reservation_id"] : "";
$guid = isset($_REQUEST["guid"]) ? $_REQUEST["guid"] : "";
$error = isset($_REQUEST["error"]) ? $_REQUEST["error"] : "";
$paid = isset($_REQUEST["paid"]) ? $_REQUEST["paid"] : "0";
$payMethod = isset($_REQUEST["payMethod"]) ? $_REQUEST["payMethod"] : "ideal";
$auto_wassen_pakket = isset($_REQUEST["auto_wassen_pakket"]) ? $_REQUEST["auto_wassen_pakket"] : "";
$auto_wassen_pakket_nr;
$auto_wassen_pakket_desc;
$auto_wassen_pakket_price;
$auto_wassen_pakket_price_display;
autoWassenPakketSwitch();

$params = "?reservation_id=$reservation_id&guid=$guid";
$cancelUrl = $idealRedirectBaseUrl."/20_ideal_cancel.php".$params;
$successUrl = $idealRedirectBaseUrl."/20_ideal_success.php".$params;
$errorUrl = $idealRedirectBaseUrl."/20_ideal_error.php".$params;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // redirect to the same page without the POST data
  header("Location: ".$_SERVER['PHP_SELF']."?redirect=redirect&date_str=$date_str&reservation_id=$reservation_id&guid=$guid&error=$error&paid=$paid&payMethod=$payMethod&auto_wassen_pakket=$auto_wassen_pakket");
  die;

} else if (!isset($_REQUEST["redirect"])) {
  // confirm reservation
  $sql = "";
  $sql .= "UPDATE RESERVATIONS ";
  $sql .= " SET CONFIRMED = 1  ";
  $sql .= "WHERE ID = '$reservation_id' ";
  $sql .= "AND GUID = '$guid' ";
  
  conn();
  //echo $sql;
  $result = mysql_query($sql);
  if (!$result) {die('Could not exec query: ' . mysql_error());}

  $sql = "";
  $sql .= "SELECT r.ID, r.START, r.SIGN_IN_EMAIL, CAST(r.CONFIRMED AS unsigned int) CONFIRMED, CAST(r.PAID AS unsigned int) PAID, rcw.ITEM_NR ";
  $sql .= "FROM RESERVATIONS r ";
  $sql .= "  INNER JOIN RESERVATIONS_CAR_WASH rcw ON r.ID = rcw.RESERVATION_ID ";
  $sql .= "WHERE r.ID = '$reservation_id' ";
  $sql .= "AND r.GUID = '$guid' ";
  
  //echo $sql;
  $result = mysql_query($sql);
  if (!$result) {die('Could not exec query: ' . mysql_error());}
  $row = mysql_fetch_array($result, MYSQL_ASSOC);
  
  if (isset($row["START"]) && $row["START"] != null) {
  
    $start = strtotime( $row["START"] );
    $reservation_date = new DateTimeDutch("@".$start, $timezone);
    $reservation_date->setTimezone($timezone); 
    $date_str = $reservation_date->format('l d F Y');
    $paid = $row["PAID"];
    //echo $paid;
    
    $auto_wassen_pakket = $row["ITEM_NR"];
  } 
  else 
  {
    $error = "Het kan zijn dat je tijdelijke reservering is verlopen. ";
    $error .= "Je kan een nieuwe reservering maken en deze bevestigen. ";
  }
  close();
  
  // redirect to the same page without the POST data
  header("Location: ".$_SERVER['PHP_SELF']."?redirect=redirect&date_str=$date_str&reservation_id=$reservation_id&guid=$guid&error=$error&paid=$paid&payMethod=$payMethod&auto_wassen_pakket=$auto_wassen_pakket");
  die;
}

?>

<?php 
include 'common/page.begin.inc.php';
?>

<body>

<div id="wrapper">

  <?php 
  $menuSelected = "diensten";
  $subMenuSelected = "auto-wassen";
  include 'common/body.header.inc.php';
  ?>

  <div id="page" class="container">
  
    <div id="content">

      <?php
      $contentPath = "Auto wassen &gt;&gt; Afspraak Maken";
      include 'common/body.content.header.inc.php';
      ?>

      <div class="main-top">
        <div class="first auto-wassen">
        </div>  
        <div class="second">
          <div class="content-blok" style="position: relative">

            <?php if (!empty($error)) { ?>
            
              <h2>Je reservering kon niet worden bevestigd</h2>
              
              <?php 
              include 'common/body.error.inc.php';
              ?>
              
            <?php } else if ($payMethod == "contant") { ?>
            
              <h2>Hartelijk dank!</h2>
              
              <p>
                Je hebt er voor gekozen om het bedrag contant af te rekenen bij de dienstverlener. 
              </p>
              
            <?php } else if ($payMethod == "pin") { ?>
            
              <h2>Hartelijk dank!</h2>
              
              <p>
                Je PIN betaling is bevestigd. 
              </p>
              
            <?php } else { ?>
            
              <?php 
              //$vcs_link_page = "26_reservation_vcs.php";
              //include 'common/outlook.link.php';
              ?>
            
              <form method="post" id="main_form_payment" action="<?php echo $idealUrl?>"> 
                <input type="hidden" name="merchantID" value="002043069" />  
                <input type="hidden" name="subID" value="0" /> 
                <input type="hidden" name="amount" value="<?php echo $auto_wassen_pakket_price; ?>" /> 
                <input type="hidden" name="purchaseID" value="<?php echo $reservation_id?>" /> 
                <input type="hidden" name="language" value="nl" /> 
                <input type="hidden" name="currency" value="EUR" />
                <input type="hidden" name="description" value="Betaling naar -Red Je Zaterdag-" />
                <input type="hidden" name="itemNumber1" value="<?php echo $auto_wassen_pakket_nr; ?>" /> 
                <input type="hidden" name="itemDescription1" value="<?php echo $auto_wassen_pakket_desc; ?>" /> 
                <input type="hidden" name="itemQuantity1" value="1" /> 
                <input type="hidden" name="itemPrice1" value="<?php echo $auto_wassen_pakket_price; ?>" /> 
                <input type="hidden" name="paymentType" value="ideal" /> 
                <input type="hidden" name="validUntil" value="2016-01-01T12:00:00" /> 
                 
                <input type="hidden" name="urlCancel" value="<?php echo $cancelUrl; ?>" /> 
                <input type="hidden" name="urlSuccess" value="<?php echo $successUrl; ?>" /> 
                <input type="hidden" name="urlError" value="<?php echo $errorUrl; ?>" /> 

                <input type="hidden" name="idealRedirectBaseUrl" value="<?php echo $idealRedirectBaseUrl; ?>" /> 
                <input type="hidden" name="date_str" value="<?php echo $date_str; ?>" /> 
                <input type="hidden" name="reservation_id" value="<?php echo $reservation_id; ?>" /> 
                <input type="hidden" name="guid" value="<?php echo $guid; ?>" /> 
                
                <input type="hidden" name="main_form_contant_action" id="main_form_contant_action" value="<?php echo $_SERVER['PHP_SELF'];?>" />
                <input type="hidden" name="main_form_contant_price_display" id="main_form_contant_price_display" value="<?php echo $auto_wassen_pakket_price_display ?>" />

                <h2>De reservering is vastgelegd</h2>

                <p>
                  <?php echo $date_str; ?>.
                </p>
                
                <?php if (!empty($paid) && ($paid == "1" || $paid == 1)) { ?>
                
                  <p>
                    De reserving is reeds betaald via iDEAL.
                  </p>
                  
                <?php } else { ?>
                
                  <p>
                    Selecteer hieronder de wijze van betaling.
                  </p>
                  
                  <p>
                    <label style="float:left; width: 50px" for="payMethod1">
                      <img src="img/ideal-logo.png" width="35" height="32"  />
                    </label>
                    <input style="float:left" type="radio" name="payMethod" value="ideal" id="payMethod1" <?php if ($payMethod=='ideal') { echo "checked=ckecked"; } ?> />
                    <label style="float:left" for="payMethod1">
                      iDEAL (&euro; <?php echo $auto_wassen_pakket_price_display ?>)
                    </label>
                   
                    <div style="clear: both; margin-bottom: 10px"></div>
                    
                    <label style="float:left; width: 50px" for="payMethod2">
                      <img src="img/contant_logo.png" width="47" height="32"  />
                    </label>
                    <input style="float:left" type="radio" name="payMethod" value="contant" id="payMethod2" <?php if ($payMethod=='contant') { echo "checked=ckecked"; } ?> />
                    <label style="float:left" for="payMethod2">
                      CONTANT (&euro; <?php echo $auto_wassen_pakket_price_display ?>)
                    </label>
                    
                    <div style="clear: both"></div>
                  </p>
                  
                  <p class="links" id="idealButton">
                    <a href="#" class="buttonBig submit">Klik hier</a>
                  </p>
                  
                <?php } ?>
                
                <div style="clear: both"></div>
              
              </form>
              
            <?php } ?>
          
          </div><!-- end content blok -->  
        </div><!-- end second -->  
      </div><!-- end home top -->
            
    </div><!-- end content -->

    <div style="clear: both;"></div>
	</div><!-- end page -->  
  
  <?php 
  include 'common/body.footer.inc.php';
  ?>
  
</div><!-- end wrapper -->

<?php 
include 'common/page.end.inc.php';
?>


